# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.exceptions import DropItem
from cassandra.cluster import Cluster


class CleanPipeline(object):
    def __init__(self):
        self.has = set()

    ##检查是否是重复数据
    def process_item(self, item, spider):
        if item.keys() >= 8:
            raise DropItem("Duplicate item found: %s" % item)
        else:
            self.has.add(item)
            return item


class CassandraPipeline(object):
    def __init__(self, dbpool):
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, settings):
        dbArgs = dict(
            host=settings['CASSANDRA_HOST'],
            # db=settings['CASSANDRA_DBNAME'],
            # user=settings['CASSANDRA_USER'],
            # password=settings['CASSANDRA_PASSWORD'],
            port=9042
        )
        dbpool = Cluster(contact_points=['localhost'],port=9042).connect('taobaob2b')
        return cls(dbpool)

    def process_item(self, item, spider):
        if self.dbpool:
            d = self.__do_insert__(self.dbpool, item, spider)
            if d:
                return True
            else:
                spider.log("Insert db failed!!!!!!!")
        else:
            spider.log("Db instence none!!!!!!!")
        return item

    def __do_insert__(self, conn, item, spider):
        try:
            sql = "INSERT INTO users (name, href, category, contact, address, numPerDay, customPerPrice) " \
                  "VALUES (%(name)s, %(href)s, %(category)s, %(contact)s, %(address)s, %(numPerDay)s, %(customPerPrice)s)"
            conn.execute(sql, {'name': item['name'], 'href': item['href'], 'category': item['category'],
                               'contact': item['contact'], 'address': item['address'], 'numPerDay': item['numPerDay'],
                               'customPerPrice': item['customPerPrice']})
        except Exception as e:
            spider.log("insert db error:" + str(e))
            raise DropItem("insert db error:" + str(e))

